<!doctype html>
<html lang="en" class="page-type-section">
<head prefix="og: http://ogp.me/ns#">
<meta charset="utf-8">
<title>空白处理 - FreeMarker 手册</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="format-detection" content="telephone=no">
<meta property="og:site_name" content="FreeMarker 手册">
<meta property="og:title" content="空白处理">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://freemarker.org/docs/dgui_misc_whitespace.html">
<link rel="canoical" href="http://freemarker.org/docs/dgui_misc_whitespace.html">
<link rel="icon" href="favicon.png" type="image/png">
<link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css">
</head>
<body itemscope itemtype="https://schema.org/Code">
    <meta itemprop="url" content="http://freemarker.org/docs/">
    <meta itemprop="name" content="FreeMarker 手册">

  <!--[if lte IE 9]>
  <div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div>
  <![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://freemarker.org" role="banner">            <img itemprop="image" src="logo.png" alt="FreeMarker">
</a><ul class="tabs"><li><a href="http://freemarker.org/">Home</a></li><li class="current"><a href="index.html">Manual</a></li><li><a class="external" href="http://freemarker.org/docs/api/index.html">Java API</a></li></ul><ul class="secondary-tabs"><li><a class="tab icon-heart" href="http://freemarker.org/contribute.html" title="Contribute"><span>Contribute</span></a></li><li><a class="tab icon-bug" href="https://sourceforge.net/p/freemarker/bugs/new/" title="Report a Bug"><span>Report a Bug</span></a></li><li><a class="tab icon-download" href="http://freemarker.org/freemarkerdownload.html" title="Download"><span>Download</span></a></li></ul></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="toc.html" class="navigation-header">Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="toc.html"><span itemprop="name">FreeMarker 手册</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui.html"><span itemprop="name">模板开发指南</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui_misc.html"><span itemprop="name">其它</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui_misc_whitespace.html"><span itemprop="name">空白处理</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Alpha. index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="dgui_template_exp.html#exp_cheatsheet">Expressions</a></li><li><a href="ref_builtins_alphaidx.html">?builtins</a></li><li><a href="ref_directive_alphaidx.html">#directives</a></li><li><a href="ref_specvar.html">.spec_vars</a></li><li><a href="app_faq.html">FAQ</a></li></ul></div></div></div>    <div class="main-content site-width">
      <div class="content-wrapper">
  <div id="table-of-contents-wrapper" class="col-left">
      <script>var breadcrumb = ["FreeMarker 手册","模板开发指南","其它","空白处理"];</script>
      <script src="toc.js"></script>
      <script src="docgen-resources/main.min.js"></script>
  </div>
<div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="dgui_misc_namespace.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_misc_alternativesyntax.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="dgui_misc_whitespace" itemprop="headline">空白处理</h1>
</div></div><div class="page-menu">
<div class="page-menu-title">Page Contents</div>
<ul><li><a class="page-menu-link" href="#dgui_misc_whitespace_stripping" data-menu-target="dgui_misc_whitespace_stripping">剥离空白</a></li><li><a class="page-menu-link" href="#autoid_28" data-menu-target="autoid_28">使用compress指令</a></li></ul> </div><p>在运行中，模板中的 <a href="gloss.html#gloss.whiteSpace">空白</a> 
		处理在某种程度上来说是纠缠所有模板引擎的一个问题。</p><p>我们来看这个模板。我已经用颜色标记了模板中的组件：
		<span class="marked-text">文本</span>，
		<span class="marked-interpolation">插值</span>，
		<span class="marked-ftl-tag">FTL 标签</span>.。
		使用 <em><span class="marked-invisible-text">[BR]</span></em> 来想象 
		<a href="gloss.html#gloss.lineBreak">换行</a>。</p>

<div class="code-wrapper"><pre class="code-block code-template"><span class="marked-text">&lt;p&gt;List of users:<em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-ftl-tag">&lt;#assign users = [{&quot;name&quot;:&quot;Joe&quot;,        &quot;hidden&quot;:false},<em><span class="marked-invisible-text">[BR]</span></em>
                  {&quot;name&quot;:&quot;James Bond&quot;, &quot;hidden&quot;:true},<em><span class="marked-invisible-text">[BR]</span></em>
                  {&quot;name&quot;:&quot;Julia&quot;,      &quot;hidden&quot;:false}]&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
&lt;ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-ftl-tag">&lt;#list users as user&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
  <span class="marked-ftl-tag">&lt;#if !user.hidden&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
  &lt;li&gt;<span class="marked-interpolation">${user.name}</span><em><span class="marked-invisible-text">[BR]</span></em>
  <span class="marked-ftl-tag">&lt;/#if&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-ftl-tag">&lt;/#list&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
&lt;/ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;p&gt;That&#39;s all.</span></pre></div><p>如果 FreeMarker 能按照规则输出所有的 
		<span class="marked-text">文本</span>，那将会输出：</p>

<div class="code-wrapper"><pre class="code-block code-output"><span class="marked-text">&lt;p&gt;List of users:<em><span class="marked-invisible-text">[BR]</span></em>
<em><span class="marked-invisible-text">[BR]</span></em>
&lt;ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
<em><span class="marked-invisible-text">[BR]</span></em>
  <em><span class="marked-invisible-text">[BR]</span></em>
  &lt;li&gt;</span>Joe<span class="marked-text"><em><span class="marked-invisible-text">[BR]</span></em>
  <em><span class="marked-invisible-text">[BR]</span></em>
<em><span class="marked-invisible-text">[BR]</span></em>
  <em><span class="marked-invisible-text">[BR]</span></em>
<em><span class="marked-invisible-text">[BR]</span></em>
  <em><span class="marked-invisible-text">[BR]</span></em>  
  &lt;li&gt;</span>Julia<span class="marked-text"><em><span class="marked-invisible-text">[BR]</span></em>
  <em><span class="marked-invisible-text">[BR]</span></em>
<em><span class="marked-invisible-text">[BR]</span></em>
&lt;/ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;p&gt;That&#39;s all.</span></pre></div><p>这里有太多的不想要的空格和换行了。幸运的是，HTML和XML对空白都不是敏感的，
		但是这么多多余的空白是很令人头疼的，而且处理后的HTML文件大小增加也是没必要的。
		当然，对于空白敏感方式的输出这依旧是个大问题。</p><p>FreeMarker 提供下面的工具来处理这个问题：</p><ul>
          <li>
            <p>忽略某些模板文件的空白的工具
			<span class="marked-for-programmers">(解析阶段空白就被移除了)</span>：</p>

            <ul>
              <li>
                <p>剥离空白：这个特性会自动忽略在FTL标签周围多余的空白。
				这个特性可以通过模板来随时启用和禁用。</p>
              </li>

              <li>
                <p>微调指令：<code class="inline-code">t</code>，<code class="inline-code">rt</code>， 
				<code class="inline-code">lt</code>。使用这些指令可以明确地告诉 FreeMarker 去忽略某些空白。
				可以阅读 <a href="ref_directive_t.html#ref.directive.t">参考手册</a> 来获取更多信息。</p>
              </li>

              <li>
                <p><a href="ref_directive_ftl.html#ref.directive.ftl"><code>ftl</code></a> 参数 
				<code class="inline-code">strip_text</code>：这将从模板中删除所有顶级文本。
				对于只包含定义宏的模板来说很有用(还有其它一些没有输出的指令)，
				因为它可以移除宏定义和其他顶级指令中的换行符，
				这样可以提高模板的可读性。</p>
              </li>
            </ul>
          </li>

          <li>
            <p>从输出中移除空白的工具 
			<span class="marked-for-programmers">(移除临近的空白)</span>：</p>

            <ul>
              <li>
                <p><code class="inline-code">compress</code> 指令。</p>
              </li>
            </ul>
          </li>
        </ul>
          



<h2 class="content-header header-section2" id="dgui_misc_whitespace_stripping">剥离空白</h2>


          

          <p>如果对模板开启这个特性，那么它就会自动忽略
		  (也就是不在输出中打印出来)两种典型的多余空白：</p>

          <ul>
            <li>
              <p>缩进空白和在行末尾的尾部空白(包括换行符)在只包含FTL标签的行中会被忽略
			  (比如 <code class="inline-code">&lt;@myMacro/&gt;</code>, <code class="inline-code">&lt;#if
              <em class="code-color">...</em>&gt;</code>)和FTL注释(如 <code class="inline-code">&lt;#-- blah --&gt;</code>)，
			  除了被忽略的空白本身。例如，如果一行只包含一个<code class="inline-code">&lt;#if
              <em class="code-color">...</em>&gt;</code>，
			  那么在标签前面的缩进和标签后面的换行符将会被忽略。
			  但是，如果这行上包含<code class="inline-code">&lt;#if <em class="code-color">...</em>&gt;x</code>，
			  那么空白就不会被忽略，因为这个<code class="inline-code">x</code>不是FTL标签。请注意，根据这些规则，
			  一行上包含<code class="inline-code">&lt;#if <em class="code-color">...</em>&gt;&lt;#list
              <em class="code-color">...</em>&gt;</code>，空白就会被忽略，
			  而一行上有<code class="inline-code">&lt;#if <em class="code-color">...</em>&gt; &lt;#list
              <em class="code-color">...</em>&gt;</code>这样的就不会，
			  因为在两个FTL标签之间的空白是嵌入的空白，而不是缩进的或尾部空白。</p>
            </li>

            <li>
              <p>加在下面这些指令之间的空白会被忽略：<code class="inline-code">macro</code>，
			  <code class="inline-code">function</code>，<code class="inline-code">assign</code>， 
			  <code class="inline-code">global</code>，<code class="inline-code">local</code>， 
			  <code class="inline-code">ftl</code>， <code class="inline-code">import</code>，
			  但也是指令之间<em>仅仅</em>只有一个空白或FTL注释。实际应用中，
			  它意味着你可以在宏定义和参数定义之间放置空行，因为行间距是为了更好的可读性，
			  不包括在输出中打印不必要的空行(换行符)。</p>
            </li>
          </ul>

          <p>使用了剥离空白后，上面那个例子中的输出就会是：</p>

          

<div class="code-wrapper"><pre class="code-block code-output"><span class="marked-text">&lt;p&gt;List of users:<em><span class="marked-invisible-text">[BR]</span></em>
&lt;ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
  &lt;li&gt;</span>Joe<span class="marked-text"><em><span class="marked-invisible-text">[BR]</span></em>
  &lt;li&gt;</span>Julia<span class="marked-text"><em><span class="marked-invisible-text">[BR]</span></em>
&lt;/ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;p&gt;That&#39;s all.</span></pre></div>

          <p>这是因为在剥离之后，模板就变成这样了，被忽略的文本没有被<span class="marked-text">标色</span>：</p>

          

<div class="code-wrapper"><pre class="code-block code-template"><span class="marked-text">&lt;p&gt;List of users:<em><span class="marked-invisible-text">[BR]</span></em></span>
<span class="marked-ftl-tag">&lt;#assign users = [{&quot;name&quot;:&quot;Joe&quot;,        &quot;hidden&quot;:false},<em><span class="marked-invisible-text">[BR]</span></em>
                  {&quot;name&quot;:&quot;James Bond&quot;, &quot;hidden&quot;:true},<em><span class="marked-invisible-text">[BR]</span></em>
                  {&quot;name&quot;:&quot;Julia&quot;,      &quot;hidden&quot;:false}]&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-text">&lt;ul&gt;<em><span class="marked-invisible-text">[BR]</span></em></span>
<span class="marked-ftl-tag">&lt;#list users as user&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
  <span class="marked-ftl-tag">&lt;#if !user.hidden&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-text">  &lt;li&gt;<span class="marked-interpolation">${user.name}</span><em><span class="marked-invisible-text">[BR]</span></em></span>
  <span class="marked-ftl-tag">&lt;/#if&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-ftl-tag">&lt;/#list&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-text">&lt;/ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;p&gt;That&#39;s all.</span></pre></div>

          <p>剥离空白功能可以通过 <a href="ref_directive_ftl.html#ref.directive.ftl"><code>ftl</code> 指令</a>
		  在模板中开启或关闭。如果你没有通过 <code class="inline-code">ftl</code> 指令来指定，
		  那么剥离空白功能是开启还是关闭就依据程序员是如何设置 FreeMarker 的了。
		  默认的情况下剥离空白是开启的，程序员可以留着不管(<span class="marked-for-programmers">建议这样做</span>)。<span class="marked-for-programmers">请注意开启剥离空白时 <em>不会</em> 
		  降低模板执行的效率，剥离空白的操作在模板加载时就已经完成了。</span></p>

          <p>剥离空白可以为单独的一行关闭，就是使用<a href="ref_directive_nt.html#ref.directive.nt"><code>nt</code></a>
		  指令(对没有去掉空白的行来说)。</p>
        
          



<h2 class="content-header header-section2" id="autoid_28">使用compress指令</h2>


          

          <p>另外一种方法就是使用<a href="ref_directive_compress.html#ref.directive.compress"><code>compress</code>
          指令</a>。和剥离空白相反，这个工作是直接基于生成的输出内容，
		  而不是对于模板进行。也就是说，它会动态地检查输出内容，
		  而不会检查生成输出FTL的程序。它会很强势地移除缩进，
		  空行和重复的空格/制表符(可以阅读<a href="ref_directive_compress.html#ref.directive.compress">参考手册</a>
		  部分来获取更多信息)。所以对于下面这段代码：</p>

          

<div class="code-wrapper"><pre class="code-block code-template"><strong>&lt;#compress&gt;</strong>
&lt;#assign users = [{&quot;name&quot;:&quot;Joe&quot;,        &quot;hidden&quot;:false},
                  {&quot;name&quot;:&quot;James Bond&quot;, &quot;hidden&quot;:true},
                  {&quot;name&quot;:&quot;Julia&quot;,      &quot;hidden&quot;:false}]&gt;
List of users:
&lt;#list users as user&gt;
  &lt;#if !user.hidden&gt;
  - ${user.name}
  &lt;/#if&gt;
&lt;/#list&gt;
That&#39;s all.
<strong>&lt;/#compress&gt;</strong></pre></div>

          <p>将会输出：</p>

          

<div class="code-wrapper"><pre class="code-block code-output">List of users:
- Joe
- Julia
That&#39;s all.</pre></div>

          <p>请注意 <code class="inline-code">compress</code> 是完全独立于剥离空白特性的。
		  所以它剥离模板中的空白是可能的，那么之后输出的内容就是被 
		  <code class="inline-code">压缩</code> 过的。</p>

          <p>此外，在默认情况下，名为 <code class="inline-code">compress</code> 
		  的用户自定义指令是可以在数据模型中存在的(由于向下兼容特性)。
		  这和指令是相同的，除了可以选择设置 <code class="inline-code">single_line</code> 参数，
		  这将会移除所有的介于其中的换行符。在最后那个例子中，如果使用<code class="inline-code">&lt;@compress
          single_line=true&gt;<em class="code-color">...</em>&lt;/@compress&gt;</code>
		  来代替<code class="inline-code">&lt;#compress&gt;<em class="code-color">...</em>&lt;/#compress&gt;</code>，
		  那么就会得到如下输出：</p>

          

<div class="code-wrapper"><pre class="code-block code-output">List of users: - Joe - Julia That&#39;s all.</pre></div>
        <div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="dgui_misc_namespace.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_misc_alternativesyntax.html"><span>Next</span></a></div></div></div></div>      </div>
    </div>
<div class="site-footer"><div class="site-width"><div class="footer-top"><div class="col-left sitemap"><div class="column"><h3 class="column-header">Overview</h3><ul><li><a href="http://freemarker.org/index.html">What is FreeMarker?</a></li><li><a href="http://freemarker.org/freemarkerdownload.html">Download</a></li><li><a href="app_versions.html">Version history</a></li><li><a href="http://freemarker.org/history.html">About us</a></li><li><a itemprop="license" href="app_license.html">License</a></li></ul></div><div class="column"><h3 class="column-header">Handy stuff</h3><ul><li><a href="http://freemarker-online.kenshoo.com/">Try template online</a></li><li><a href="dgui_template_exp.html#exp_cheatsheet">Expressions cheatsheet</a></li><li><a href="ref_directive_alphaidx.html">#directives</a></li><li><a href="ref_builtins_alphaidx.html">?built_ins</a></li><li><a href="ref_specvar.html">.special_vars</a></li></ul></div><div class="column"><h3 class="column-header">Community</h3><ul><li><a href="https://github.com/nanlei/freemarker/tree/manual-zh-2.3-gae/src/manual">Chinese Manual on Github</a></li><li><a href="https://github.com/freemarker/freemarker">FreeMarker on Github</a></li><li><a href="https://twitter.com/freemarker">Follow us on Twitter</a></li><li><a href="https://sourceforge.net/p/freemarker/bugs/new/">Report a bug</a></li><li><a href="http://stackoverflow.com/questions/ask?tags=freemarker">Ask a question</a></li><li><a href="http://freemarker.org/mailing-lists.html">Mailing lists</a></li></ul></div></div><div class="col-right"><ul class="social-icons"><li><a class="github" href="https://github.com/freemarker/freemarker">Github</a></li><li><a class="twitter" href="https://twitter.com/freemarker">Twitter</a></li><li><a class="stack-overflow" href="http://stackoverflow.com/questions/ask?tags=freemarker">Stack Overflow</a></li></ul><a class="xxe" href="http://www.xmlmind.com/xmleditor/" rel="nofollow" title="Edited with XMLMind XML Editor"><span>Edited with XMLMind XML Editor</span></a></div></div><div class="footer-bottom"><p><span class="generated-for-product">Generated for: Freemarker 2.3.23</span><span class="last-updated"> Last generated:
<time itemprop="dateModified" datetime="2015-09-18T14:38:51Z" title="Friday, September 18, 2015 2:38:51 PM GMT">2015-09-18 14:38:51 GMT</time></span></p> <p class="copyright">
© <span itemprop="copyrightYear">1999</span>–2015
<a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="http://freemarker.org">The FreeMarker Project</a>. All rights reserved. </p>
</div></div></div></body>
</html>
